﻿using Arc.DairyMan.DairyMan.BusinessLogic;
using Arc.DairyMan.DairyMan.Model;
using Arc.WinForms.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Arc.DairyMan.DairyMan.UI
{
    public partial class frmSettings : ArcForm
    {
        #region Variables & Properties
        DataSet _DS = new DataSet();
        Settings Model;
        blSettings BL;
        #endregion


        public frmSettings()
        {
            InitializeComponent();
            BL = new blSettings();
            Model = (Settings)BL.ModelObj;
            BL.DS = _DS;
            BL.Search("1<>1");

            ResetForm();
        }

        #region Private Methods
        void ResetForm()
        {
            foreach (Control ctl in this.Controls.GetControls<ArcTextBox>())
                ctl.Text = string.Empty;

            BL.Select("SettingsId = 1");
            if (_DS.Tables.Contains("Settings") && _DS.Tables["Settings"].Rows.Count > 0)
            {
                DataRow _dr = _DS.Tables["Settings"].Rows[0];
                Model.SettingsId = Convert.ToInt32(_dr["SettingsId"]);
                txtFatRate.Text = _dr["FatRate"].ToString();
                txtNote.Text = _dr["Note"].ToString();
                txtSlipHeading.Text = _dr["SlipHeading"].ToString();
            }

            Model.SetDefaultValues();
            txtFatRate.Focus();
        }
        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ValidateForm();
                Model.FatRate = Convert.ToDecimal(txtFatRate.Text);
                Model.Note = txtNote.Text;
                Model.SlipHeading = txtSlipHeading.Text;

                BL.Transaction(true);

                MessageBox.Show("Settings Saved");
                ResetForm();
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void ValidateForm()
        {
            decimal _outFatRate;
            if (string.IsNullOrEmpty(txtFatRate.Text))
            {
                txtFatRate.Focus();
                throw new Exception("Fat Rate should not be blank.");
            }
            else if (Convert.ToDecimal(txtFatRate.Text) ==0)
            {
                txtFatRate.Focus();
                throw new Exception("Fat Rate should be greater than 0.");
            }
            //else if (string.IsNullOrEmpty(txtSlipHeading.Text))
            //{
            //    txtSlipHeading.Focus();
            //    throw new Exception("Slip Heading should not be blank.");
            //}
        }


        bool rateHasDot = false;
        private void txtFatRate_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b')
            {
                // Allow Digits and BackSpace char 
            }
            else if (e.KeyChar == '.' && !rateHasDot)
            {
                //Allows only one Dot Char 
                rateHasDot = true;
            }
            else { e.Handled = true; }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
